/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ifc.model.converter;

import ifconnect.model.dao.CursoDao;
import ifconnect.model.entity.Curso;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author Genemes
 */
@FacesConverter(forClass = Curso.class)
public class CursoConverter implements Converter {

    private CursoDao dao = new CursoDao();
    

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        try {
            String atributo = "nome";
            Curso c = dao.porcurarPorAtributo(atributo, value);
            if(c == null){
                FacesMessage mensagem = new FacesMessage("Curso inválido");
                mensagem.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ConverterException(mensagem);
            }
            return c;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object value) {
        Curso curso = (Curso) value;
        return curso.getNome();
    }
}
